package org.lex.filter.swing;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import org.lex.filter.BooleanFilter;
import org.lex.filter.Filter;
import org.lex.filter.Filters;


public class FileTypeFilterPane extends AbstractFilterPane<Boolean> {
	private static final String[] TYPES = { "Files and Folders", "Files Only", "Folders Only" };

	private JPanel fileTypePane = null;
	private JLabel generalTypeLabel = null;
	private JComboBox generalTypeComboBox = null;
	private JPanel gluePane = null;
	private JPanel gluePane2 = null;

	public FileTypeFilterPane() {
		super();
		initialize();
	}

	public String getTitle() {
		return null;
	}

	public Filter getFilter() {
		Filter filter = null;
		int type = this.getGeneralTypeComboBox().getSelectedIndex();
		if (0 == type)
			filter = Filters.acceptAllFilter();
		else if (1 == type)
			filter = new BooleanFilter(false, TYPES[1]);
		else if (2 == type)
			filter = new BooleanFilter(true, TYPES[2]);
		return filter;
	}

	// =======================
	private void initialize() {
		GridBagConstraints gridBagConstraints41 = new GridBagConstraints();
		gridBagConstraints41.gridx = 0;
		gridBagConstraints41.gridwidth = 3;
		gridBagConstraints41.fill = GridBagConstraints.BOTH;
		gridBagConstraints41.weightx = 1.0;
		gridBagConstraints41.weighty = 1.0;
		gridBagConstraints41.gridy = 1;
		GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
		gridBagConstraints3.gridx = 0;
		gridBagConstraints3.gridwidth = 3;
		gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints3.weightx = 1.0;
		gridBagConstraints3.insets = new Insets(0, 6, 0, 5);
		gridBagConstraints3.gridy = 0;
		this.setSize(300, 200);
		this.setLayout(new GridBagLayout());
		this.add(getFileTypePane(), gridBagConstraints3);
		this.add(getGluePane(), gridBagConstraints41);
	}

	private JPanel getFileTypePane() {
		if (fileTypePane == null) {
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 2;
			gridBagConstraints6.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints6.weightx = 1.0;
			gridBagConstraints6.gridy = 0;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.fill = GridBagConstraints.NONE;
			gridBagConstraints5.gridy = 0;
			gridBagConstraints5.weightx = 0.0;
			gridBagConstraints5.insets = new Insets(0, 6, 5, 5);
			gridBagConstraints5.anchor = GridBagConstraints.WEST;
			gridBagConstraints5.ipadx = 60;
			gridBagConstraints5.gridx = 1;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.insets = new Insets(0, 6, 5, 0);
			gridBagConstraints4.anchor = GridBagConstraints.WEST;
			gridBagConstraints4.gridy = 0;
			generalTypeLabel = new JLabel();
			generalTypeLabel.setText("Type");
			fileTypePane = new JPanel();
			fileTypePane.setLayout(new GridBagLayout());
			fileTypePane.setBorder(BorderFactory.createTitledBorder(null, "File Type", TitledBorder.DEFAULT_JUSTIFICATION,
					TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			fileTypePane.add(generalTypeLabel, gridBagConstraints4);
			fileTypePane.add(getGeneralTypeComboBox(), gridBagConstraints5);
			fileTypePane.add(getGluePane2(), gridBagConstraints6);
		}
		return fileTypePane;
	}

	private JComboBox getGeneralTypeComboBox() {
		if (generalTypeComboBox == null) {
			generalTypeComboBox = new JComboBox();
			for (String type : TYPES)
				generalTypeComboBox.addItem(type);
			generalTypeComboBox.setSelectedIndex(0);
		}
		return generalTypeComboBox;
	}

	private JPanel getGluePane() {
		if (gluePane == null) {
			gluePane = new JPanel();
		}
		return gluePane;
	}

	private JPanel getGluePane2() {
		if (gluePane2 == null) {
			gluePane2 = new JPanel();
		}
		return gluePane2;
	}
}
